package org.lsms.servlet;

/**
 * servlet used to invite reviewer
 * @author ZhanXueliang
 * 2010.8.23
 */

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import javax.mail.*;

import org.lsms.bean.User;
import org.lsms.dao.impl.DaoImpl;
import org.lsms.mail.MailSenderInfo;
import org.lsms.mail.SimpleMailSender;

public class invite_reviewer extends HttpServlet {

	
	public void destroy() {
		super.destroy(); 
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			
			DaoImpl impl = new DaoImpl();
			
			HttpSession session = request.getSession();
			int member_id = Integer.parseInt(session.getAttribute("userid").toString());
		
			String loginname = request.getParameter("loginname");
			String to = request.getParameter("email");
			String firstname = request.getParameter("firstname");
			String middlename = request.getParameter("middlename");
			String lastname = request.getParameter("lastname");
			
			User user = new User();
			user.setLoginname(loginname);
			user.setFirstname(firstname);
			user.setEmail(to);
			user.setMiddlename(middlename);
			user.setLastname(lastname);
			user.setPassword("123456");
			user.setPermission(2);
			user.setLogintimes(0);
			
			MailSenderInfo mailInfo = new MailSenderInfo();
			mailInfo.init(mailInfo);
			mailInfo.setToAddress(to);
			mailInfo.setSubject("LSMS & ICSEE 2010 Reviewer Invite E-Mail");
			
			String content = "Dear Mr/Mrs " + firstname+" "+middlename+" "+lastname+'\n'+'\n';
			String letter_content = impl.get_mail_content(1);
			content +=letter_content;
			String ps = "\nPS:\nYour Loginname:"+loginname+"  \nYour Password:123456";
			content +=ps;
			mailInfo.setContent(content);
			
			SimpleMailSender sms = new SimpleMailSender();
			
			if(impl.check_login_name(loginname)){
				if(impl.signup(user) && sms.sendTextMail(mailInfo)){
					int reviewer_id = impl.get_user_id(loginname);
					if(impl.add_relation_tie(member_id, reviewer_id)){
						String member_info = "The invite email has been sent to "+to;
						response.sendRedirect("member/member_show_info.jsp?member_info="+member_info);
					}else{
						String member_info = "Problem occurs when inviting PC member.\n" +
								"Please try again later.";
						response.sendRedirect("member/member_show_info.jsp?member_info="+member_info);
					}	
				}else{
					String member_info = "Problem occurs when inviting PC member.\n" +
					"Please try again later.";
					response.sendRedirect("member/member_show_info.jsp?member_info="+member_info);
				}
			}else{
				String member_info = "The login name "+loginname+" has been used!\n" +
						"Please choose anothor one.";
				response.sendRedirect("member/member_show_info.jsp?member_info="+member_info);
			}
			
			
			
	}

	public void init() throws ServletException {
		// Put your code here
	}

}
